package com.orangesoft.tescherfriend.login;


import com.orangesoft.common.config.WebSessionManager;
import com.orangesoft.common.constant.Constants;
import com.orangesoft.tescherfriend.business.user.entity.User;
import com.orangesoft.tescherfriend.business.user.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 此类主要是处理管理员登录
 *
 * @author xhy
 */
@Controller
public class LoginAction {

    @Autowired
    protected WebSessionManager webSessionManager;

    @Autowired
    protected UserService userService;

    /**
     * 主页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView indexMain(HttpServletRequest request) {//设置模板参数
        String ctx = request.getContextPath();
        Map<String, String> dataMap = new HashMap<>();
//        dataMap.put("themePath", ctx + "/invoice");
        dataMap.put("ctxPath", ctx);
        dataMap.put("msg", "");
        return new ModelAndView("/index", dataMap);
    }

    /**
     * 管理人员登录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/toLogin")
    public ModelAndView login(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String username = request.getParameter("username");
        if (username == null || StringUtils.isEmpty(username.trim())) {
            map.put("msg", "登录名为空,请重新输入！");
            return new ModelAndView("forward:/index", map);
        }
        String pwd = request.getParameter("password").trim();
        if (pwd == null || StringUtils.isEmpty(pwd.trim())) {
            map.put("msg", "密码为空,请重新输入！");
            return new ModelAndView("forward:/index", map);
        }
        String pwdmd5 = DigestUtils.md5Hex(pwd.trim()); // 获取当前用户
        List<User> userList = userService.findByUserCode(username);
        if (userList == null || userList.isEmpty()) {
            map.put("msg", "无此用户！");
            return new ModelAndView("forward:/index", map);
        }
        if (userList.size() > 1) {
            map.put("msg", "用户异常，联系管理员！");
            return new ModelAndView("forward:/index", map);
        }
        User user = userList.get(0);
        // 如果当前用户未进行认证
        if (true) {
            Subject currentUser = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username,
                    pwdmd5);
            // 登录认证 记录登陆信息
            currentUser.login(token);
            webSessionManager.setSessionValue(request, Constants.SESSION_USERCODE, user.getUserCode());
            webSessionManager.setSessionValue(request, Constants.SESSION_USERNAME, user.getUserName());
            webSessionManager.setSessionValue(request, Constants.SESSION_LOGOURL, user.getLogoUrl());
            return new ModelAndView("/main");
        }
        map.put("msg", "登录名或者密码有错误,请重新输入！");
        return new ModelAndView("forward:/index", map);
    }

    /**
     * 管理人员登录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/platform/login2")
    public ModelAndView login2(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String username = request.getParameter("username");
        if (username == null || "".equals(username.trim())) {
            map.put("msg", "登录名为空,请重新输入！");
            return new ModelAndView("forward:/index", map);
        }
        String pwd = request.getParameter("password").trim();
        if (pwd == null || "".equals(pwd.trim())) {
            map.put("msg", "密码为空,请重新输入！");
            return new ModelAndView("forward:/index", map);
        }
        String pwdmd5 = DigestUtils.md5Hex(pwd);
//        SystemUser systemUser = systemUserService.getByUserCodeAndPwd(
//                username, pwdmd5);
//        if (systemUser != null) {
//            Subject currentUser = SecurityUtils.getSubject();
//            UsernamePasswordToken token = new UsernamePasswordToken(username,
//                    pwdmd5);
//            // 登录认证 记录登陆信息
//            currentUser.login(token);
//            webSessionManager.setSessionValue(request, Constants.SYSTEM_USER_CODE, systemUser.getUserCode());
//            webSessionManager.setSessionValue(request, Constants.SYSTEM_USER_NAMA, systemUser.getUserName());
        return new ModelAndView("/main");
//        } else {
//            map.put("msg", "登录名或者密码有错误,请重新输入！");
//            return new ModelAndView("forward:/index", map);
//        }
    }
//
//    /***
//     * 平台管理人员登出
//     *
//     * @param request
//     * @return
//     */
//    @RequestMapping(value = "/logout")
//    public String logout(HttpServletRequest request) {
//        Subject currentUser = SecurityUtils.getSubject();
//        //shiro登录销毁
//        currentUser.logout();
//        return "redirect:/index";
//    }
}
